﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models;
using JNPF.Basics.Models.System.Dtos.DictionaryType;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller.System
{
    /// <summary>
    /// 用户管理
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.09.21 
    /// </summary>
    [Route("api/System/[controller]")]
    public class DictionaryTypeController : BaseApiController
    {
        private DictionaryTypeBll dictionaryTypeBll = new DictionaryTypeBll();

        #region GET
        /// <summary>
        /// 获取字典分类
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseResult> GetList()
        {
            var data = await dictionaryTypeBll.GetList();
            var treeList = data.MapToList<DictionaryTypeListOutput>();
            return Success(new { list = treeList.ToTree("-1") });

        }

        /// <summary>
        ///获取所有字典分类下拉框列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("Selector")]
        public async Task<ResponseResult> GetSelector()
        {
            var data = await dictionaryTypeBll.GetList();
            var treeList = data.MapToList<DictionaryTypeSelectorOutput>();
            return Success(new { list = treeList.ToTree("-1") });

        }

        /// <summary>
        /// 获取字典分类信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ResponseResult> GetInfo(string id)
        {
            var data = await dictionaryTypeBll.GetInfo(id);
            var output = data.MapTo<DictionaryTypeInfoOutput>();
            return Success(output);
        }
        #endregion

        #region Post
        /// <summary>
        /// 添加字典分类
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Creater([FromBody] DictionaryTypeCrInput input)
        {
            if (await dictionaryTypeBll.IsExistByEnCode(input.enCode, null))
            {
                return Error("编码已存在");
            }
            if (await dictionaryTypeBll.IsExistByFullName(input.fullName, null))
            {
                return Error("名称已存在");
            }
            var entity = input.MapTo<DictionaryTypeEntity>();
            var isOk = await dictionaryTypeBll.Create(entity);
            return isOk > 0 ? Success("新建成功") : Error("新建失败");
        }

        /// <summary>
        /// 修改字典分类
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(string id, [FromBody] DictionaryTypeUpInput input)
        {
            if (await dictionaryTypeBll.IsExistByEnCode(input.enCode, id))
            {
                return Error("编码已存在");
            }
            if (await dictionaryTypeBll.IsExistByFullName(input.fullName, id))
            {
                return Error("名称已存在");
            }
            var entity = input.MapTo<DictionaryTypeEntity>();
            var isOk = await dictionaryTypeBll.Update(id, entity);
            return isOk > 0 ? Success("修改成功") : Error("修改失败");
        }

        /// <summary>
        /// 删除字典分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(string id)
        {
            var entity = await dictionaryTypeBll.GetInfo(id);
            await dictionaryTypeBll.Delete(entity);
            return Success("删除成功");
        }
        #endregion
    }
}
